home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / DirectX8Importer.py < prev    next >
Text File  |  2009-08-31  |  7KB  |  239 lines

  1. #!BPY
  2.  
  3. """ Registration info for Blender menus:
  4. Name: 'DirectX(.x)...'
  5. Blender: 244
  6. Group: 'Import'
  7.  
  8. Tip: 'Import from DirectX text file format format.'
  9. """
  10. # DirectXImporter.py version 1.2
  11. # Copyright (C) 2005  Arben OMARI -- omariarben@everyday.com 
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU General Public License for more details.
  22.  
  23. # This script import meshes from DirectX text file format
  24.  
  25. # Grab the latest version here :www.omariben.too.it
  26. import bpy
  27. import Blender
  28. from Blender import Mesh,Object,Material,Texture,Image,Draw
  29.  
  30.  
  31. class xImport:
  32.     def __init__(self, filename):
  33.         global my_path
  34.         self.file = open(filename, "r")
  35.         my_path = Blender.sys.dirname(filename)
  36.  
  37.         # 
  38.         self.lines = [l_split for l in self.file.readlines() for l_split in (' '.join(l.split()),) if l_split]
  39.  
  40.     def Import(self):
  41.         lines = self.lines
  42.         print "importing into Blender ..."
  43.         scene  = bpy.data.scenes.active
  44.         
  45.         mesh_indicies = {} # the index of each 'Mesh' is used as the key for those meshes indicies
  46.         context_indicies = None # will raise an error if used!
  47.         
  48.         
  49.         #Get the line of Texture Coords
  50.         nr_uv_ind = 0
  51.  
  52.         #Get Materials
  53.         nr_fac_mat = 0
  54.         i = -1
  55.         mat_list = []
  56.         tex_list = []
  57.         mesh_line_indicies = []
  58.         for j, line in enumerate(lines):
  59.             l = line.strip()
  60.             words = line.split()
  61.             if words[0] == "Material" :
  62.                 #context_indicies["Material"] = j
  63.                 self.loadMaterials(j, mat_list, tex_list)
  64.             elif words[0] == "MeshTextureCoords" :
  65.                 context_indicies["MeshTextureCoords"] = j
  66.                 #nr_uv_ind = j
  67.             elif words[0] == "MeshMaterialList" :
  68.                 context_indicies["MeshMaterialList"] = j+2
  69.                 #nr_fac_mat = j + 2
  70.             elif words[0] == "Mesh": # Avoid a second loop
  71.                 context_indicies = mesh_indicies[j] = {'MeshTextureCoords':0, 'MeshMaterialList':0}
  72.         
  73.         for mesh_index, value in mesh_indicies.iteritems():
  74.             mesh = Mesh.New()
  75.             self.loadVertices(mesh_index, mesh, value['MeshTextureCoords'], value['MeshMaterialList'], tex_list)
  76.             
  77.             mesh.materials = mat_list[:16]
  78.             if value['MeshMaterialList']:
  79.                 self.loadMeshMaterials(value['MeshMaterialList'], mesh)
  80.             scene.objects.new(mesh)
  81.             
  82.         self.file.close()
  83.         print "... finished"
  84.  
  85.     #------------------------------------------------------------------------------
  86.     #        CREATE THE MESH
  87.     #------------------------------------------------------------------------------
  88.     def loadVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
  89.         v_ind = nr_vr_ind + 1
  90.         lin = self.lines[v_ind]
  91.         if lin :
  92.             lin_c = self.CleanLine(lin)
  93.             nr_vert = int((lin_c.split()[0]))
  94.         else :
  95.             v_ind = nr_vr_ind + 2
  96.             lin = self.lines[v_ind]
  97.             lin_c = self.CleanLine(lin)
  98.             nr_vert = int((lin_c.split()[0]))
  99.  
  100.         #--------------------------------------------------
  101.         nr_fac_li = v_ind + nr_vert +1
  102.         lin_f = self.lines[nr_fac_li]
  103.         if lin_f :
  104.             lin_fc = self.CleanLine(lin_f)
  105.             nr_face = int((lin_fc.split()[0]))
  106.         else :
  107.             nr_fac_li = v_ind + nr_vert +1
  108.             lin_f = self.lines[nr_fac_li]
  109.             lin_fc = self.CleanLine(lin_f)
  110.             nr_face = int((lin_fc.split()[0]))
  111.  
  112.         #Get Coordinates
  113.         verts_list = [(0,0,0)] # WARNING - DUMMY VERT - solves EEKADOODLE ERROR
  114.         for l in xrange(v_ind + 1, (v_ind + nr_vert +1)):
  115.             line_v = self.lines[l]
  116.             lin_v = self.CleanLine(line_v)
  117.             words = lin_v.split()
  118.             if len(words)==3:
  119.                 verts_list.append((float(words[0]),float(words[1]),float(words[2])))
  120.         
  121.         mesh.verts.extend(verts_list)
  122.         del verts_list
  123.         
  124.         face_list = []
  125.         #Make Faces
  126.         i = 0
  127.         mesh_verts = mesh.verts
  128.         for f in xrange(nr_fac_li + 1, (nr_fac_li + nr_face + 1)):
  129.             i += 1
  130.             line_f = self.lines[f]
  131.             lin_f = self.CleanLine(line_f)
  132.             
  133.             # +1 for dummy vert only!
  134.             words = lin_f.split()
  135.             if len(words) == 5:
  136.                 face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3]), 1+int(words[4])))
  137.             elif len(words) == 4:
  138.                 face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3])))
  139.         
  140.         mesh.faces.extend(face_list)
  141.         del face_list
  142.         
  143.         if nr_uv :
  144.             mesh.faceUV = True
  145.             for f in mesh.faces:
  146.                 fuv = f.uv
  147.                 for ii, v in enumerate(f):
  148.                     # _u, _v = self.CleanLine(self.lines[nr_uv + 2 + v.index]).split()
  149.                     
  150.                     # Use a dummy vert
  151.                     _u, _v = self.CleanLine(self.lines[nr_uv + 1 + v.index]).split()
  152.                     
  153.                     fuv[ii].x = float(_u)
  154.                     fuv[ii].y = float(_v)
  155.             
  156.                 if nr_fac_mat :
  157.                     fac_line = self.lines[nr_fac_mat + i]
  158.                     fixed_fac = self.CleanLine(fac_line)
  159.                     w_tex = int(fixed_fac.split()[0])
  160.                     f.image = tex_list[w_tex]
  161.                     
  162.         # remove dummy vert
  163.         mesh.verts.delete([0,])
  164.         
  165.     def CleanLine(self,line):
  166.         return line.replace(\
  167.             ";", " ").replace(\
  168.             '"', ' ').replace(\
  169.             "{", " ").replace(\
  170.             "}", " ").replace(\
  171.             ",", " ").replace(\
  172.             "'", " ")
  173.  
  174.     #------------------------------------------------------------------
  175.     # CREATE MATERIALS
  176.     #------------------------------------------------------------------
  177.     def loadMaterials(self, nr_mat, mat_list, tex_list):
  178.         
  179.         def load_image(name):
  180.             try:
  181.                 return Image.Load(Blender.sys.join(my_path,name))
  182.             except:
  183.                 return None
  184.         
  185.         mat = bpy.data.materials.new()
  186.         line = self.lines[nr_mat + 1]
  187.         fixed_line = self.CleanLine(line)
  188.         words = fixed_line.split()
  189.         mat.rgbCol = [float(words[0]),float(words[1]),float(words[2])]
  190.         mat.setAlpha(float(words[3]))
  191.         mat_list.append(mat)
  192.         l = self.lines[nr_mat + 5]
  193.         fix_3_line = self.CleanLine(l)
  194.         tex_n = fix_3_line.split()
  195.         
  196.         if tex_n and tex_n[0] == "TextureFilename" :
  197.  
  198.             if len(tex_n) > 1:
  199.                 tex_list.append(load_image(tex_n[1]))
  200.  
  201.             if len(tex_n) <= 1 :
  202.  
  203.                 l_succ = self.lines[nr_mat + 6]
  204.                 fix_3_succ = self.CleanLine(l_succ)
  205.                 tex_n_succ = fix_3_succ.split()
  206.                 tex_list.append(load_image(tex_n_succ[0]))
  207.         else :
  208.             tex_list.append(None) # no texture for this index
  209.  
  210.         return mat_list, tex_list
  211.     #------------------------------------------------------------------
  212.     # SET MATERIALS
  213.     #------------------------------------------------------------------
  214.     def loadMeshMaterials(self, nr_fc_mat, mesh):
  215.         for face in mesh.faces:
  216.             nr_fc_mat += 1
  217.             line = self.lines[nr_fc_mat]
  218.             fixed_line = self.CleanLine(line)
  219.             wrd = fixed_line.split()
  220.             mat_idx = int(wrd[0])
  221.             face.mat = mat_idx
  222.  
  223. #------------------------------------------------------------------
  224. #  MAIN
  225. #------------------------------------------------------------------
  226. def my_callback(filename):
  227.     if not filename.lower().endswith('.x'): print "Not an .x file" 
  228.     ximport = xImport(filename)
  229.     ximport.Import()
  230.  
  231. arg = __script__['arg']
  232.  
  233. if __name__ == '__main__':
  234.     Blender.Window.FileSelector(my_callback, "Import DirectX", "*.x")
  235.  
  236. #my_callback('/fe/x/directxterrain.x')
  237. #my_callback('/fe/x/Male_Normal_MAX.X')
  238. #my_callback('/fe/x/male_ms3d.x')
  239.